using System;
using System.Collections.Generic;
using System.Text;
using ConsoleApplication1.Data;
using MongoDB;

namespace ConsoleApplication1
{
    static class DataAccess
    {
        public static Context Context = new Context();

        public static void AddState(int mapId, List<PlaceState> states)
        {
            Log.WriteLine("Adding a state to map {0}", mapId);

            ParkingState state = new ParkingState();
            state.MapId = mapId;
            state.StateTime = DateTime.Now;
            state.States = states;

            Context.DB.GetCollection<ParkingState>().Insert(new ParkingState[] { state });
        }
        public static ParkingState GetLatestState(int mapId)
        {
            IMongoCollection<ParkingState> coll = Context.DB.GetCollection<ParkingState>();
            ICursor<ParkingState> cur = coll.FindAll().Sort("StateTime", IndexOrder.Descending).Limit(1);
            IEnumerator<ParkingState> enu = cur.Documents.GetEnumerator();
            if (enu.MoveNext())
                return enu.Current;
            else return null;
        }
        public static ParkingMap GetMap(int id)
        {
            IMongoCollection<ParkingMap> coll = Context.DB.GetCollection<ParkingMap>();
            ParkingMap map = coll.FindOne(new Document("_id", id));
            return map;
        }
    }
}
